package testBox2D 
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2Fixture;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author ...
	 */
	public class __test__ extends Sprite
	{
		private var _world:b2World;
		private var _startingVelocity:b2Vec2;
		
		private var _wheelBodyDef:b2BodyDef;
		private var _wheelBody:b2Body;
		private var _circleShape:b2CircleShape;
		private var _wheelFixtureDef:b2FixtureDef;
		private var _wheelFixture:b2Fixture;
		
		private var _groundBodyDef:b2BodyDef;
		private var _groundBody:b2Body;
		private var _groundShape:b2PolygonShape;
		private var _groundFixtureDef:b2FixtureDef;
		private var _groundFixture:b2Fixture;
		
		private var _wheelArray:Array;
		private var _stepTime:Timer;
		private var _sTick:Shape;
		
		public function __test__() 
		{
			var __gravity:b2Vec2 = new b2Vec2(0, 10);
			_world = new b2World(__gravity, true);
			_wheelArray = new Array;
			_startingVelocity = new b2Vec2(50, 0);
			
			_wheelBodyDef = new b2BodyDef;
			_wheelBodyDef.type = b2Body.b2_dynamicBody;
			_wheelBody = _world.CreateBody(_wheelBodyDef);
			_wheelBody.SetLinearVelocity(_startingVelocity);
			_circleShape = new b2CircleShape(5);
			_wheelFixtureDef = new b2FixtureDef;
			_wheelFixtureDef.shape = _circleShape;
			_wheelFixture = _wheelBody.CreateFixture(_wheelFixtureDef);
			_wheelBody.SetPosition(new b2Vec2(50, 50));
			
			_groundBodyDef = new b2BodyDef;
			_groundBodyDef.position.Set(0, stage.stageHeight);
			_groundBody = _world.CreateBody(_groundBodyDef);
			_groundShape = new b2PolygonShape;
			_groundShape.SetAsBox(stage.stageWidth, 1);
			_groundFixtureDef = new b2FixtureDef;
			_groundFixtureDef.shape = _groundShape;
			_groundFixture = _groundBody.CreateFixture(_groundFixtureDef); 
			
			var _rightWallBodyDef:b2BodyDef = new b2BodyDef();
			_rightWallBodyDef.position.Set(stage.stageWidth, 0);
			var _rightWallBody:b2Body = _world.CreateBody(_rightWallBodyDef);
			var _rightWallShape:b2PolygonShape = new b2PolygonShape();
			_rightWallShape.SetAsBox(1, stage.stageHeight);
			var _rightWallFixtureDef:b2FixtureDef = new b2FixtureDef();
			_rightWallFixtureDef.shape = _rightWallShape;
			var _rightWallFixture:b2Fixture = _rightWallBody.CreateFixture(_rightWallFixtureDef);

			var _leftWallBodyDef:b2BodyDef = new b2BodyDef();
			_leftWallBodyDef.position.Set(0, 0);
			var _leftWallBody:b2Body = _world.CreateBody(_leftWallBodyDef);
			var _leftWallShape:b2PolygonShape = new b2PolygonShape();
			_leftWallShape.SetAsBox(1, stage.stageHeight);
			var _leftWallFixtureDef:b2FixtureDef = new b2FixtureDef();
			_leftWallFixtureDef.shape = _leftWallShape;
			var _leftWallFixture:b2Fixture = _leftWallBody.CreateFixture(_leftWallFixtureDef);

			var _ceilingBodyDef:b2BodyDef = new b2BodyDef();
			_ceilingBodyDef.position.Set(0, 0);
			var _ceilingBody:b2Body = _world.CreateBody(_ceilingBodyDef);
			var _ceilingShape:b2PolygonShape = new b2PolygonShape();
			_ceilingShape.SetAsBox(stage.stageWidth, 1);
			var _ceilingFixtureDef:b2FixtureDef = new b2FixtureDef();
			_ceilingFixtureDef.shape = _ceilingShape;
			var _ceilingFixture:b2Fixture = _ceilingBody.CreateFixture(_ceilingFixtureDef);
			
			
			_sTick = new Shape;
			addChild(_sTick);
			
			_stepTime = new Timer(0.025 * 1000);
			_stepTime.addEventListener(TimerEvent.TIMER, _onTick, false, 0, true);
			//trace(this, "x:", _wheelBody.GetPosition().x, "y:", _wheelBody.GetPosition().y);
			_stepTime.start();
			
			
		}
		
		private function _onTick(e:TimerEvent):void 
		{
			_sTick.graphics.clear();
			_sTick.graphics.lineStyle(3, 0xFFFF00);
			//_sTick.graphics.lineTo(_wheelBody.GetPosition().x, _wheelBody.GetPosition().y);
			_world.Step(0.025, 10, 10);
			_sTick.graphics.drawCircle(_wheelBody.GetPosition().x, _wheelBody.GetPosition().y, 5);
			
		}
		
	}

}